#pragma once

#include <QString>
#include <vector>
#include <unordered_map>
#include <QtCore/qglobal.h>
#include <QMetaType>
#include "ThostFtdcMdApi.h"

// DLL 导出宏定义
#if defined(Q_OS_WIN)
    #if defined(CTPQUOTEENGINE_LIBRARY)
        #define QUOTE_EXPORT Q_DECL_EXPORT
    #else
        #define QUOTE_EXPORT Q_DECL_IMPORT
    #endif
#else
    #define QUOTE_EXPORT
#endif

namespace QuoteEngine {

// CTP API 创建函数指针类型定义
typedef CThostFtdcMdApi* (*CreateFtdcMdApiFunc)(const char*, bool, bool);

// CTP连接状态
enum class ConnectionStatus {
    Disconnected,    // 未连接/已断开
    Connecting,      // 连接中
    Connected,       // 已连接
    Disconnecting    // 断开中
};

// 使用静态map存储状态描述
inline const QString& getConnectionStatusString(ConnectionStatus status)
{
    static const std::unordered_map<ConnectionStatus, QString> statusMap{
        {ConnectionStatus::Disconnected,  "Disconnected"},
        {ConnectionStatus::Connecting,    "Connecting"},
        {ConnectionStatus::Connected,     "Connected"},
        {ConnectionStatus::Disconnecting, "Disconnecting"}
    };
    
    static const QString unknown = "Unknown";
    auto it = statusMap.find(status);
    return it != statusMap.end() ? it->second : unknown;
}

struct QuoteConfig {
    QString brokerID;              // 经纪商ID
    QString userID;                // 用户ID
    QString password;              // 密码
    QString frontAddress;          // 前置机地址
    QString mdDllPath;             // CTP行情库路径
    QString flowPath;              // 流数据路径
    std::vector<QString> symbols;  // 订阅的合约列表
    
    struct {
        bool enableUdp{false};     // 是否启用UDP
        int heartbeatTimeout{15};  // 心跳超时时间(秒)
    } connection;
    
    struct {
        QString path;              // 日志路径
        QString level{"info"};     // 日志级别
    } log;
};

} // namespace QuoteEngine

// 在命名空间外注册类型
Q_DECLARE_METATYPE(QuoteEngine::ConnectionStatus)